package com.toolbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class toolbar extends AppCompatActivity {
    private Toolbar mMyTb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);

        mMyTb = findViewById(R.id.my_tb);
        setSupportActionBar(mMyTb);//不加这句无菜单!!!
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.tab_add:
                Toast.makeText(toolbar.this, "tab_add", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tab_about:
                Toast.makeText(toolbar.this, "tab_about", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tab_before:
                Toast.makeText(toolbar.this, "tab_before", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
